<?php
/**
 * Hy - 系统登录、框架、欢迎页模块
 *
 * Copyright (c) 2013 YinHailin All rights reserved.
 * @Author: YinHailin
 * @Authorize: Boren Network
 * @Created by YinHailin at 2013-11-20
 */


defined('LY_VERSION') or exit();

class IndexMod extends PublicMod {

  /* 后台入口 */
  public function Index() {
    header('Location:/admin.php?m=index&a=login');
  }

  /* 后台登录 */
  public function Login() {
    parent::GetGlobal();  //调用公共方法
    parent::CheckLogin(true);  //检查登录

    $objAdmin = M(self::$dataAdmin);

    if ($_POST) {
      $POST = deep_htmlspecialchars_decode($_POST);
      parent::CheckField('admin_name', $POST['loginname'], 'require', '请输入用户名');
      parent::CheckField('password', $POST['password'], 'require', '请输入密码');
      parent::CheckField('checkcode', $POST['checkcode'], 'uniform', '验证码错误', $_SESSION['checkcode']);
      $find = $objAdmin->Where('admin_name = "'.$POST['loginname'].'" AND `password` = "'.get_password($POST['password']).'"')->Find();
      if ($find > 0) {
        $status = parent::SetAdminLogin(1, $find['id']);
        if ($status == '111') {
          $this->Success('登录成功，欢迎回来：'.$find['admin_name'], '/admin.php?m=index&a=admin');
        } else {
          $this->Error('系统繁忙，请稍后再试');
        }
      } else {
        $this->Error('用户名或密码错误');
      }
    } else {
      //导航栏选中状态
      $this->Assign('navNo', 2);

      //标题输出
      $this->Assign('pageTitle', '登录 - '.C('SYS_NAME').'v'.C('SYS_VERSION'));

      $this->Display();
    }
  }

  /* 强制登录 */
  public function Force() {
    parent::GetGlobal();  //调用公共方法

    $status = parent::SetAdminLogin(2, intval($_COOKIE[C('ADMIN_NAME').'_key']));
    echo '<script type="text/javascript">location.replace("/'.$_GET['url'].'");</script>';
  }

  /* 后台框架 */
  public function Admin() {
    parent::GetGlobal();  //调用公共方法
    parent::CheckLogin();  //检查登录

    $this->Assign('pageTitle', C('SYS_NAME').' '.C('SYS_VERSION'));

    $this->Display();
  }

  /* 后台顶部 */
  public function Top() {
    parent::GetGlobal();  //调用公共方法
    parent::CheckLogin();  //检查登录

    $this->Assign('admin', parent::GetActiveUser());

    //获取顶部导航栏
    $topMenu = rabbet('menu_other', 'top');
    //去除key值重复的菜单
    static $_trueMenu = array();
    foreach ($topMenu as $key => $value) {
      if (in_array($value['key'], $_trueMenu)) {
        unset($topMenu[$key]);
      } else {
        $_trueMenu[] = $value['key'];
      }
    }
    $topMenu = asort_array($topMenu, 'sequence');
    $this->Assign('topMenu', $topMenu);

    $this->Assign('pageTitle', C('SYS_NAME').' '.C('SYS_VERSION'));

    $this->Display();
  }

  /* 后台侧边 */
  public function Menu() {
    parent::GetGlobal();  //调用公共方法
    parent::CheckLogin();  //检查登录

    $route = deep_htmlspecialchars($_GET['route']);
    $this->Assign('route', $route);

    //获取顶部导航栏
    $sideMenu = rabbet('menu_other', 'side');
    foreach ($sideMenu as $key => $value) {
      $sideMenu[$key] = asort_array($sideMenu[$key], 'sequence');
    }
    $this->Assign('sideMenu', $sideMenu);

    $this->Assign('pageTitle', C('SYS_NAME').' '.C('SYS_VERSION'));

    $this->Display();
  }

  /* 后台底部 */
  public function Bottom() {
    parent::GetGlobal();  //调用公共方法
    parent::CheckLogin();  //检查登录

    $this->Assign('pageTitle', C('SYS_NAME').' '.C('SYS_VERSION'));

    $this->Display();
  }

  /* 后台首页 */
  public function Main() {
    parent::GetGlobal();  //调用公共方法
    parent::CheckLogin();  //检查登录

    $this->Assign('admin', parent::GetActiveUser());

    $phpsys = array(
      'php_system'=>C('SYS_NAME').' '.C('SYS_VERSION'),
      'php_uname'=>php_uname(),
      'php_gd'=>function_exists('gd_info'),
      'php_version'=>PHP_VERSION,
      'php_port'=>$_SERVER['SERVER_PORT'],
      'php_software'=>$_SERVER['SERVER_SOFTWARE'],
      'php_max_time'=>get_cfg_var('max_execution_time').'s',
      'php_sapi_name'=>php_sapi_name(),
    );
    $this->Assign('phpsys',$phpsys);

    $this->Assign('pageTitle', C('SYS_NAME').' '.C('SYS_VERSION'));

    $this->Display();
  }

  /* 退出系统 */
  public function SetLogout() {
    parent::GetGlobal();  //调用公共方法

    $activeUser = parent::GetActiveUser();

    parent::SetAdminLogin(0, $activeUser['id']);
    echo '<script type="text/javascript">top.location.replace("/admin.php");</script>';
  }

  /* 清空缓存 */
  public function ClearCache() {
    $runtimePath=ROOT_PATH.'Runtime/';
    /* 清空后台缓存 */
    dir_delete($runtimePath.'Admin/');
    /* 清空前台缓存 */
    dir_delete($runtimePath.'Index/');
    /* 清空上帝之门缓存 */
    dir_delete($runtimePath.'God/');
    /* 清空其它缓存 */
    dir_delete($runtimePath.'Plugin/');

    $this->Success('缓存更新已完成！','/admin.php?m=index&a=main');
  }
}